﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ICE.Vacations.Logica.Consultas;
using ICE.Vacations.Logica.Solicitudes;
using ICE.Vacations.Entidades;

namespace ICE.Vacations.WebASP
{
    public partial class aprobaciones : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                cargarSolicitudes();
            }
            catch
            {
                Response.Redirect("Default.aspx", true);
            }
        }



        public void cargarSolicitudes()
        {
            if (!filtro.Text.Trim().Equals(""))
            {

                    if (listaCampos.SelectedValue.Equals("Nombre"))
                        listaSolicitudesXAprobar.DataSource = QueryManager.consultaSolicitudesXAprobador((string)Session["username"], "", "", filtro.Text);
                    else
                        listaSolicitudesXAprobar.DataSource = QueryManager.consultaSolicitudesXAprobador((string)Session["username"], listaCampos.SelectedValue, filtro.Text);

           

            }
            else
            {
                listaSolicitudesXAprobar.DataSource = QueryManager.consultaSolicitudesXAprobador((string)Session["username"]);

            }

            listaSolicitudesXAprobar.DataBind();
        }

        protected void filtro_TextChanged(object sender, EventArgs e)
        {

        }

        protected void listaSol_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int indice = Int32.Parse(e.CommandArgument.ToString());
            if (e.CommandName.Equals("aprobarSolicitud"))
            {
                //Response.Write("<script>alert('This is Alert1');</script>");
                //RequestManager.aprobarSolicitud((string)Session["username"], QueryManager.getSolicitudes()[indice], true);
                RequestManager.aprobarSolicitud((string)Session["username"], QueryManager.getSolicitudes()[indice], false);
                string message = "Se ha rechazado la solicitud";
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("<script type = 'text/javascript'>");
                sb.Append("window.onload=function(){");
                sb.Append("alert('");
                sb.Append(message);
                sb.Append("');");
                sb.Append("window.location.href = 'aprobaciones.aspx';};");
                sb.Append("</script>");
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
              
               // Response.Redirect("aprobaciones.aspx");
               // System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('Se ha aprobado la solicitud');", true);
            }
            else if (e.CommandName.Equals("rechazarSolicitud"))
            {
                RequestManager.aprobarSolicitud((string)Session["username"], QueryManager.getSolicitudes()[indice], false);
                string message = "Se ha rechazado la solicitud";
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("<script type = 'text/javascript'>");
                sb.Append("window.onload=function(){");
                sb.Append("alert('");
                sb.Append(message);
                sb.Append("');");
                sb.Append("window.location.href = 'aprobaciones.aspx';};");
                sb.Append("</script>");
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
              
                //System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert('Hello this is an Alert')</SCRIPT>");
                //Response.Redirect("aprobaciones.aspx");

                //System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('Se ha rechazado la solicitud');", true);
                
            }

            else if (e.CommandName.Equals("verSolicitud"))
            {
                try
                {

                    Solicitud sol = QueryManager.getSolicitudes()[indice];
                    Response.ContentType = "application/pdf";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=" + sol.requestID + "_" + sol.employeeID + ".pdf");
                    Response.TransmitFile(Server.MapPath("~/PDF/" + sol.requestID + "_" + sol.employeeID + ".pdf"));
                    Response.End();
                }
                catch (Exception ex)
                {
                    System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('No se encuentra el documento');", true);
                }

            }
        }
    }
}